package com.umeox.watch.moto.launcher.keyboard;

import android.content.Context;
import android.inputmethodservice.KeyboardView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.umeox.moto.watch.themes.widget.RelativeLayout;
import com.umeox.watch.moto.launcher.R;

import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

/**
 * Created by Rambo 2024/3/12/0012
 * Describe:
 */
public class InputMethodView extends RelativeLayout implements View.OnClickListener {

    private int currentKeyboardType;

    private boolean isShifted;

    private KeyboardView.OnKeyboardActionListener mOnKeyActionListener;

    private LinearLayout mBottomLl;
    private LinearLayout mContentLl;

    public final int getKeyboardType() {
        return currentKeyboardType;
    }

    public final void setKeyboardType(int type) {
        currentKeyboardType = type;
        if (type == -101) {
            //切换成标点符号
            createSymbols();
        } else if (type != -100) {
            //切换成英文
            createQWER();
        } else {
            //切换成数字
            create1234();
        }
    }

    public final boolean getShifted() {
        return isShifted;
    }

    public final void setShifted(boolean shifted) {
        isShifted = shifted;
        int childCount = mContentLl.getChildCount();
        for (int i = 0; i < childCount; i++) {
            LinearLayout linearLayout = (LinearLayout) mContentLl.getChildAt(i);
            for (int j = 0; j < linearLayout.getChildCount(); j++) {
                View view = linearLayout.getChildAt(j);
                if (view instanceof TextView) {
                    TextView textView = (TextView) view;
                    CharSequence text = textView.getText();
                    textView.setText(lowerUpCase(text));
                }
            }
        }
    }

    public final void setOnKeyActionListener(KeyboardView.OnKeyboardActionListener onKeyActionListener) {
        mOnKeyActionListener = onKeyActionListener;
    }

    public final KeyboardView.OnKeyboardActionListener getOnKeyActionListener() {
        return mOnKeyActionListener;
    }


    public InputMethodView(Context context) {
        this(context, null);
    }

    public InputMethodView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InputMethodView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        currentKeyboardType = -102;
        View view = View.inflate(context, R.layout.view_input_method, this);
        mContentLl = view.findViewById(R.id.ll);
        mBottomLl = view.findViewById(R.id.ll_bottom);
        if (currentKeyboardType == -101) {
            createSymbols();
        } else if (currentKeyboardType != -100) {
            createQWER();
        } else {
            create1234();
        }
    }

    @Override
    public void onClick(View v) {
        if (v == null) {
            return;
        }
        int intValue;
        Object tag = v.getTag();
        if (tag instanceof String) {
            intValue = Integer.parseInt((String) tag);
        } else {
            intValue = (Integer) tag;
        }
        if (mOnKeyActionListener != null) {
            mOnKeyActionListener.onKey(intValue, null);
        }
    }

    private CharSequence lowerUpCase(CharSequence charSequence) {
        String result;
        if (isShifted && charSequence.length() < 3 && Character.isLowerCase(charSequence.charAt(0))) {
            String obj = charSequence.toString();
            Locale locale = Locale.getDefault();
            result = obj.toUpperCase(locale);
        } else {
            result = charSequence.toString().toLowerCase(Locale.ROOT);
        }
        return result;
    }


    private void createQWER() {
        this.mBottomLl.removeAllViews();
        this.mContentLl.removeAllViews();
        Map<Integer, String> e10 = new LinkedHashMap<Integer, String>() {{
            put(113, "q");
            put(119, "w");
            put(101, "e");
            put(114, "r");
            put(116, "t");
            put(121, "y");
            put(117, "u");
            put(105, "i");
            put(111, "o");
            put(112, "p");
        }};
        Map<Integer, String> e11 = new LinkedHashMap<Integer, String>() {{
            put(97, "a");
            put(115, "s");
            put(100, "d");
            put(102, "f");
            put(103, "g");
            put(104, "h");
            put(106, "j");
            put(107, "k");
            put(108, "l");
        }};
        Map<Integer, String> e12 = new LinkedHashMap<Integer, String>() {{
            put(-1, "ic_capslock");
            put(122, "z");
            put(120, "x");
            put(99, "c");
            put(118, "v");
            put(98, "b");
            put(110, "n");
            put(109, "m");
        }};
        Map<Integer, String> e13 = new LinkedHashMap<Integer, String>() {{
            put(-100, "123");
            put(32, "ic_space_bar");
            put(-5, "ic_backspace_keyboard");
        }};

        initView(e10, true);
        initView(e11, true);
        initView(e12, true);
        initView(e13, false);
        setShifted(false);
    }

    private void initView(Map<Integer, String> map, boolean isNotBottom) {
        LinearLayout linearLayout;
        View imageView;
        if (isNotBottom) {
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(-2, -2);
            layoutParams.topMargin = 5;
            linearLayout = (LinearLayout) View.inflate(getContext(), R.layout.view_row, null);
            this.mContentLl.addView(linearLayout, layoutParams);
        } else {
            linearLayout = this.mBottomLl;
        }
        for (Map.Entry entry : map.entrySet()) {
            int intValue = (int) entry.getKey();
            if (intValue == -5 || intValue == -1 || intValue == 32) {
                imageView = new ImageView(getContext());
                ((ImageView) imageView).setImageResource(imageView.getResources().getIdentifier((String) entry.getValue(), "drawable", imageView.getContext().getPackageName()));
                imageView.setBackgroundResource(R.drawable.ic_keyboard_bg);
                int padding = (int) dp2px(8);
                imageView.setPadding(padding, padding, padding, padding);
            } else {
                imageView = View.inflate(getContext(), R.layout.view_key, null);
                ((TextView) imageView).setText((CharSequence) entry.getValue());
            }
            imageView.setTag(intValue);
            LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams((int) dp2px(40), (int) dp2px(38));
            layoutParams2.setMarginEnd((int) dp2px(8));
            imageView.setLayoutParams(layoutParams2);
            imageView.setOnClickListener(this);
            linearLayout.addView(imageView);
        }
    }

    static void d(InputMethodView inputMethodView, Map<Integer, String> map, boolean z10, int i10, Object obj) {
        if ((i10 & 2) != 0) {
            z10 = true;
        }
        inputMethodView.initView(map, z10);
    }

    private void create1234() {
        this.mBottomLl.removeAllViews();
        this.mContentLl.removeAllViews();

        Map<Integer, String> e10 = new LinkedHashMap<Integer, String>() {{
            put(55, "7");
            put(56, "8");
            put(57, "9");
            put(-5, "ic_backspace_keyboard");
        }};
        Map<Integer, String> e11 = new LinkedHashMap<Integer, String>() {{
            put(52, "4");
            put(53, "5");
            put(54, "6");
            put(32, "ic_space_bar");
        }};

        Map<Integer, String> e12 = new LinkedHashMap<Integer, String>() {{
            put(49, "1");
            put(50, "2");
            put(51, "3");
            put(64, "@");
        }};

        Map<Integer, String> e13 = new LinkedHashMap<Integer, String>() {{
            put(-102, "abc");
            put(48, "0");
            put(-101, "!?#");
        }};


        d(this, e10, false, 2, null);
        d(this, e11, false, 2, null);
        d(this, e12, false, 2, null);
        initView(e13, false);
    }

    private void createSymbols() {
        this.mBottomLl.removeAllViews();
        this.mContentLl.removeAllViews();

        Map<Integer, String> e10 = new LinkedHashMap<Integer, String>() {{
            put(64, "@");
            put(35, "#");
            put(36, "$");
            put(37, "%");
            put(38, "&");
            put(42, "*");
            put(45, "-");
            put(61, "=");
            put(40, "(");
            put(41, ")");
        }};
        Map<Integer, String> e11 = new LinkedHashMap<Integer, String>() {{
            put(95, "_");
            put(33, "!");
            put(34, "\"");
            put(39, "'");
            put(58, ":");
            put(59, ";");
            put(47, "/");
            put(44, ",");
            put(46, ".");
            put(63, "?");
        }};

        Map<Integer, String> e12 = new LinkedHashMap<Integer, String>() {{
            put(-100, "123");
            put(32, "ic_space_bar");
            put(-5, "ic_backspace_keyboard");
        }};

        initView(e10, true);
        initView(e11, true);
        initView(e12, false);
    }

    public final float dp2px(Number number) {
        return number.floatValue() * getContext().getResources().getDisplayMetrics().density;
    }
}
